var bgColor = "#1f212d";//背景
var upColor="#F9293E";//涨颜色
var downColor="#00aa3b";//跌颜色
const downBorderColor = '#008F28';
const upBorderColor = '#8A0000';
// ma  颜色
var ma5Color = "#39afe6";
var ma10Color = "#da6ee8";
var ma20Color = "#ffab42";
var ma30Color = "#00940b";

/**
 * 15:20 时:分 格式时间增加num分钟
 * @param {Object} time 起始时间
 * @param {Object} num
 */
function addTimeStr(time,num){ 
	var hour=time.split(':')[0];
	var mins=Number(time.split(':')[1]);
	var mins_un=parseInt((mins+num)/60);
	var hour_un=parseInt((Number(hour)+mins_un)/24);
	if(mins_un>0){
		if(hour_un>0){
			var tmpVal=((Number(hour)+mins_un)%24)+"";
			hour=tmpVal.length>1? tmpVal:'0'+tmpVal;//判断是否是一位
		}else{
			var tmpVal=Number(hour)+mins_un+"";
			hour=tmpVal.length>1? tmpVal:'0'+tmpVal;
		}
		var tmpMinsVal=((mins+num)%60)+"";
		mins=tmpMinsVal.length>1? tmpMinsVal:0+tmpMinsVal;//分钟数为 取余60的数
	}else{
		var tmpMinsVal=mins+num+"";
		mins=tmpMinsVal.length>1? tmpMinsVal:'0'+tmpMinsVal;//不大于整除60
	} 
	return hour+":"+mins;
}

//获取增加指定分钟数的 数组  如 09:30增加2分钟  结果为 ['09:31','09:32'] 
function getNextTime(startTime, endTIme, offset, resultArr) {
	var result = addTimeStr(startTime, offset);
	resultArr.push(result);
	if (result == endTIme) {
		return resultArr;
	} else {
		return getNextTime(result, endTIme, offset, resultArr);
	}
}


/**
 * 不同类型的股票的交易时间会不同  
 * @param {Object} type   hs=沪深  us=美股  hk=港股
 */
var time_arr = function(type) { 
	if(type.indexOf('us')!=-1){//生成美股时间段
		var timeArr = new Array();
		timeArr.push('09:30')
		return getNextTime('09:30', '16:00', 1, timeArr);
	}
	if(type.indexOf('hs')!=-1){//生成沪深时间段
		var timeArr = new Array();
			timeArr.push('09:30');
			timeArr.concat(getNextTime('09:30', '11:29', 1, timeArr)); 
			timeArr.push('13:00');
			timeArr.concat(getNextTime('13:00', '15:00', 1, timeArr)); 
		return timeArr;
	}
	if(type.indexOf('hk')!=-1){//生成港股时间段
		var timeArr = new Array();
			timeArr.push('09:30');
			timeArr.concat(getNextTime('09:30', '11:59', 1, timeArr)); 
			timeArr.push('13:00');
			timeArr.concat(getNextTime('13:00', '16:00', 1, timeArr)); 
		return timeArr;
	}
	
}


var get_m_data = function(m_data,type) {
	var priceArr = new Array();
	var avgPrice = new Array();
	var vol = new Array();
	var times = time_arr(type); 
	$.each(m_data.data, function(i, v) {
		priceArr.push(v[1]);
		avgPrice.push(v[2]);
		vol.push(v[3]); 
	})
	return {
		priceArr: priceArr,
		avgPrice: avgPrice,
		vol: vol,
		times: times
	}
}

// 真实数据
var get_m_data2 = function (m_data,type) {
	var priceArr = new Array();
	var avgPrice = new Array();
	var vol = new Array();
	var times = time_arr(type);
	$.each(m_data.data, function(i, v) {
		const v1 =v.split(",");
		priceArr.push(v1[1]);
		vol.push(v1[2]);
		if (i === 0&&type==="us") {
			avgPrice.push(v1[1]);
		}else {
			avgPrice.push(v1[3]);
		}

	})
	return {
		priceArr: priceArr,
		avgPrice: avgPrice,
		vol: vol,
		times: times
	}
}



//==========================================分时表 option

/**
 * 生成分时option
 * @param {Object} m_data 分时数据
 * @param {Object} type 股票类型  us-美股  hs-沪深  hk-港股
 */
function initMOption(m_data,type){
	var m_datas = get_m_data2(m_data,type);
	var interval = 119;
	if (type === 'hk'){
		interval =164
	}else if (type ==='us') {
		interval =194
	}else {
		interval =119
	}

	return {
		tooltip: { //弹框指示器
			trigger: 'axis',
			axisPointer: {
				type: 'cross'
			},
			formatter: function(params, ticket, callback) {
				var i = params[0].dataIndex;

				var color;
				if (m_datas.priceArr[i] > m_data.yestclose) {
					color ='style="color:#ff4242"';
				} else {
					color ='style="color:#26bf66"';
				}

				var html = '<div class="commColor" style="width:100px;"><div>当前价 <span  '+color+' >' + m_datas.priceArr[i] + '</span></div>';
				html += '<div>均价 <span  '+color+' >' + m_datas.avgPrice[i] + '</span></div>';
				html += '<div>涨幅 <span  '+color+' >' + ratioCalculate(m_datas.priceArr[i],m_data.yestclose)+ '%</span></div>';
				html += '<div>成交量 <span  '+color+' >' + m_datas.vol[i] + '</span></div></div>'
				return html;
			}
		},
		legend: { //图例控件,点击图例控制哪些系列不显示
			icon: 'rect',
			type: 'scroll',
			itemWidth: 14,
			itemHeight: 2,
			left: 20,
			top: '-1%',
			textStyle: {
				fontSize: 12,
				color: '#0e99e2'
			}
		},
		axisPointer: {
			link: [
				{
					xAxisIndex: 'all'
				}
			],
			label: {
				backgroundColor: '#777'
			}
		},
		color: [ma5Color, ma10Color],
		grid: [{
				id: 'gd1',
				left: '4%',
				right: '4%',
				height: '80%', //主K线的高度,
				top: '8%'
			},
			{
				id: 'gd2',
				left: '4%',
				right: '4%',
				height: '80%', //主K线的高度,
				top: '8%'
			}
		],
		xAxis: [ //==== x轴
			{ //主图
				show:true,
				gridIndex: 0,
				data: m_datas.times,
				axisLabel: { //label文字设置
					color: '#9b9da9',
					fontSize: 10,
					interval:interval
				},
				splitLine: {
					show: false,

				}
			},
			{
				show:true,
				gridIndex: 1,
				data: m_datas.times,
				axisLabel: { //label文字设置
					show: false
				},
				splitLine: {
					show: false,
				}
			}
		],
		yAxis: [ //y轴
			{
				gridIndex: 0,
				scale: true,
				splitNumber: 3,
				axisLabel: { //label文字设置
					inside: true, //label文字朝内对齐
					color:function(val){
						if(val==m_data.yestclose){
							return '#aaa'
						}
						return val>m_data.yestclose? upColor:downColor;
					}
				},z:4,splitLine: { //分割线设置
					show: false,
					lineStyle: {
						color: '#181a23'
					}
				},
			}, {
				scale: true,  gridIndex: 1, splitNumber: 3,
				position: 'right', z:4,
				axisLabel: { //label文字设置
					color:function(val){
						if(val==m_data.yestclose){
							return '#aaa'
						}
						return val>m_data.yestclose? upColor:downColor;
					},
					inside: true, //label文字朝内对齐
					formatter:function(val){
						var resul=ratioCalculate(val,m_data.yestclose);
						return  Number(resul).toFixed(2)+' %'}
				},
				splitLine: { //分割线设置
					show: false,
					lineStyle: {
						color: '#181a23'
					}
				},
				axisPointer:{show:true,
					label:{
						formatter:function(params){ //计算右边Y轴对应的当前价的涨幅比例
							return  ratioCalculate(params.value,m_data.yestclose)+'%';
						}
					}
				}
			}

		],
		dataZoom: [

		],
		//animation:false,//禁止动画效果
		backgroundColor: bgColor,
		blendMode: 'source-over',
		series: [{
				name: '当前价',
				type: 'line',
				data: m_datas.priceArr,
				smooth: true,
				symbol: "circle", //中时有小圆点
				lineStyle: {
					normal: {
						opacity: 0.8,
						color: '#39afe6',
						width: 1
					}
				},

				areaStyle: {
					normal: {
						color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
							offset: 0,
							color: 'rgba(0, 136, 212, 0.7)'
						}, {
							offset: 0.8,
							color: 'rgba(0, 136, 212, 0.02)'
						}], false),
						shadowColor: 'rgba(0, 0, 0, 0.1)',
						shadowBlur: 10
					}
				}
			},
			{
				name: '均价',
				type: 'line',
				data: m_datas.avgPrice,
				smooth: true,
				symbol: "circle",
				lineStyle: { //标线的样式
					normal: {
						opacity: 0.8,
						color: '#da6ee8',
						width: 1
					}
				}
			},{
				type: 'line',
				data: m_datas.priceArr,
				smooth: true,
				symbol: "none",
				gridIndex: 1,
				xAxisIndex: 1,
				yAxisIndex: 1,

				lineStyle: { //标线的样式
					normal: {
						width: 0
					}
				}
			},

		]
	};
}



/**
 * 计算价格涨跌幅百分比
 * @param {Object} price 当前价
 * @param {Object} yclose 昨收价
 */
function ratioCalculate(price,yclose){
	return ((price-yclose)/yclose*100).toFixed(3);
}









//数组处理
function splitData(rawData) {
	var datas = []; var times = [];var vols = [];
	for (var i = 0; i < rawData.length; i++) {
		console.log(rawData[i])
		datas.push(rawData[i]);
		times.push(rawData[i].splice(0, 1)[0]);
		vols.push(rawData[i][4]);
	}
	return {datas:datas,times:times,vols:vols};
}

function splitData2(rawData) {
	var datas = [];
	var times = [];
	var vols = [];
	for (var i = 0; i < rawData.length; i++) {
		const d = rawData[i].split(",");
		datas.push([d[1],d[2],d[4],d[3]]);
		times.push(d[0]);
		vols.push(d[5]);
	}
	return {datas:datas,times:times,vols:vols};
}


//================================MA计算公式
function calculateMA(dayCount,data) {
	var result = [];
	for (var i = 0, len = data.times.length; i < len; i++) {
		if (i < dayCount) {
			result.push('-');
			continue;
		}
		var sum = 0;
		for (var j = 0; j < dayCount; j++) {
			sum += data.datas[i - j][1];
		}
		result.push((sum / dayCount).toFixed(2));
	}
	return result;
}


//=================================================MADC计算公式

var calcEMA,calcDIF,calcDEA,calcMACD;

/*
 * 计算EMA指数平滑移动平均线，用于MACD
 * @param {number} n 时间窗口
 * @param {array} data 输入数据
 * @param {string} field 计算字段配置
 */
calcEMA=function(n,data,field){
    var i,l,ema,a;
    a=2/(n+1);
    if(field){
        //二维数组
        ema=[data[0][field]];
        for(i=1,l=data.length;i<l;i++){
            ema.push((a*data[i][field]+(1-a)*ema[i-1]).toFixed(2));
        }
    }else{
        //普通一维数组
        ema=[data[0]];
        for(i=1,l=data.length;i<l;i++){
            ema.push((a*data[i]+(1-a)*ema[i-1]).toFixed(3) );
        }
    }
    return ema;
};

/*
 * 计算DIF快线，用于MACD
 * @param {number} short 快速EMA时间窗口
 * @param {number} long 慢速EMA时间窗口
 * @param {array} data 输入数据
 * @param {string} field 计算字段配置
 */
calcDIF=function(short,long,data,field){
    var i,l,dif,emaShort,emaLong;
    dif=[];
    emaShort=calcEMA(short,data,field);
    emaLong=calcEMA(long,data,field);
    for(i=0,l=data.length;i<l;i++){
        dif.push((emaShort[i]-emaLong[i]).toFixed(3));
    }
    return dif;
};

/*
 * 计算DEA慢线，用于MACD
 * @param {number} mid 对dif的时间窗口
 * @param {array} dif 输入数据
 */
calcDEA=function(mid,dif){
    return calcEMA(mid,dif);
};

/*
 * 计算MACD
 * @param {number} short 快速EMA时间窗口
 * @param {number} long 慢速EMA时间窗口
 * @param {number} mid dea时间窗口
 * @param {array} data 输入数据
 * @param {string} field 计算字段配置
 */
calcMACD=function(short,long,mid,data,field){
    var i,l,dif,dea,macd,result;
    result={};
    macd=[];
    dif=calcDIF(short,long,data,field);
    dea=calcDEA(mid,dif);
    for(i=0,l=data.length;i<l;i++){
        macd.push(((dif[i]-dea[i])*2).toFixed(3));
    }
    result.dif=dif;
    result.dea=dea;
    result.macd=macd;
    return result;
};


 //=================================================MADC计算公式 end


function initKOption(cdata){
	var data = splitData2(cdata);
	return  {
		// title: {
		// 	text: '上证指数',
		// 	left: 0
		// },
		tooltip: {
			trigger: 'axis',
			axisPointer: {
				type: 'cross'
			}
		},

		grid: {
			left: '4%',
			right: '4%',
			bottom: '25%',
		},
		xAxis: {
			type: 'category',
			data: data.times,
			scale: true,
			boundaryGap: false,
			axisLine: { onZero: false },
			splitLine: { show: false },
			axisLabel: { //label文字设置
				color: '#c7c7c7',
			},
			min: 'dataMin',
			max: 'dataMax'
		},
		yAxis: {
			scale: true,
			splitArea: {
				show: false
			},
			z:4,
			axisLabel: { //label文字设置
				color: '#c7c7c7',
				inside: true, //label文字朝内对齐
			},
			splitLine: { //分割线设置
				show: false,
				lineStyle: {
					color: '#181a23'
				}
			},
		},
		dataZoom: [{
			type: 'slider',
			xAxisIndex: [0], //控件联动
			start: 100,
			end: 80,
			throttle: 10,
			top: '85%',
			height: '10%',
			borderColor: '#696969',
			textStyle: {
				color: '#dcdcdc'
			},
			handleSize: '90%', //滑块图标
			handleIcon: 'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
			dataBackground: {
				lineStyle: {
					color: '#fff'
				}, //数据边界线样式
				areaStyle: {
					color: '#696969'
				} //数据域填充样式
			}
		},
			// 		{
			// 			type: 'inside',
			// 			xAxisIndex: [0,1,2],//控件联动
			// 		},
		],backgroundColor: bgColor,blendMode: 'source-over',
		series: [
			{
				name: '日K',
				type: 'candlestick',
				data: data.datas,
				itemStyle: {
					color: upColor,
					color0: downColor,
					borderColor: upBorderColor,
					borderColor0: downBorderColor
				},
				markPoint: {
					label: {
						formatter: function (param) {
							return param != null ? param.value + '' : '';
						}
					},
					data: [
						{
							name: 'Mark',
							coord: ['2013/5/31', 2300],
							value: 2300,
							itemStyle: {
								color: 'rgb(41,60,85)'
							}
						},
						{
							name: 'highest value',
							type: 'max',
							valueDim: 'highest'
						},
						{
							name: 'lowest value',
							type: 'min',
							valueDim: 'lowest'
						},
						{
							name: 'average value on close',
							type: 'average',
							valueDim: 'close'
						}
					],
					tooltip: {
						formatter: function (param) {
							return param.name + '<br>' + (param.data.coord || '');
						}
					}
				},
				markLine: {
					symbol: ['none', 'none'],
					label:{
							position: 'middle'
					},
					data: [
						[
							{
								name: 'from lowest to highest',
								type: 'min',
								valueDim: 'lowest',
								symbol: 'circle',
								symbolSize: 10,
								label: {
									show: false
								},
								emphasis: {
									label: {
										show: false
									}
								}
							},
							{
								type: 'max',
								valueDim: 'highest',
								symbol: 'circle',
								symbolSize: 10,
								label: {
									show: false
								},
								emphasis: {
									label: {
										show: false
									}
								}
							}
						],
						{
							name: 'min line on close',
							type: 'min',
							valueDim: 'close'
						},
						{
							name: 'max line on close',
							type: 'max',
							valueDim: 'close'
						}
					]
				}
			}
		]
	};

		
		
}
 


 